/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


 ************************************************************************************************
 Objet SPLITTER
 VERSION:0.85
 DATE:16/03/2006
 ************************************************************************************************
Init parameter in objPar (object of parameter):
 Name:               frame name
 top:           	 Top position (in pixel)
 left:               Left position (in pixel)             
 width:              Width 
 height              height
 prop				 value beetween 0 and 1 that represents the proportion of the first area of the splitter
					 (0.8 indicate that the first area represents 80% of the splitter)
 orientation  		 'v' or 'h' indicate the orientation of the splitter (horizontal or vertical)
 limInf				 number beetween 0 and 1 that represents the minimum value of the proportion	0
 limSup				 number beetween 0 and 1 that represents the maximum value of the proportion	1
 withImg			 if true, splitter have an image to start the drag an drop	false
 reverseClose		 true if the splitter close to the right or the bottom according to the orientation	false
 style				 Represents the style of the splitter. 'normal' style without decoration and '3D' style with decoration
 parent              object parent  		        
 taileCurs			 length of the cursor
*************************************************************************************************/


// *************************************
// CONSTRUCTOR
// *************************************

rialto.widget.Splitter=function (objPar)
{
	//base constructor
    this.base = rialto.widget.AbstractContainer;
    objPar.type="splitter";
    this.base(objPar);

    //Default value
    this.prop = 0.5;  
    this.orientation = 'h';         
    this.autoResizableH=true;  
    this.autoResizableW=true;  
    this.style='normal';           
    this.withImg=false;           
    this.overflow='hidden';
    this.reverseClose=false;
    this.modeLim = '%';
    this.limInf=0;
    this.limSup=1;   
    this.tailleCurs=4;
    //set the value with the properties of the objPar object
    if (rialto.lang.isNumber(objPar.prop) && objPar.prop>=0 && objPar.prop<=1){
	    this.prop = objPar.prop;    
    }
	if (rialto.lang.isStringIn(objPar.orientation,['v','h'])){
        this.orientation = objPar.orientation;        
    }
    if (rialto.lang.isBoolean(objPar.autoResizeContenu)){
        this.autoResizeContenu = objPar.autoResizeContenu;        
    }
    if (rialto.lang.isBoolean(objPar.autoResizeParent)){
        this.autoResizeParent = objPar.autoResizeParent;        
        this.autoResizableH=this.autoResizeParent;
    }        
    if (rialto.lang.isBoolean(objPar.autoResizableH)){
        this.autoResizableH = objPar.autoResizableH;        
    }
    if (rialto.lang.isBoolean(objPar.autoResizableW)){
        this.autoResizableW = objPar.autoResizableW;        
    }   
         
    if (rialto.lang.isStringIn(objPar.style,['normal','3D'])){
        this.style = objPar.style;        
    }   
    if (rialto.lang.isStringIn(objPar.overflow,["auto","hidden"])){
        this.overflow = objPar.overflow;        
    }
    if (rialto.lang.isStringIn(objPar.modeLim,["%","abs"])){    
        this.modeLim = objPar.modeLim;        
    }
    if (this.modeLim == '%') {
		if (rialto.lang.isNumber(objPar.limInf) && objPar.limInf>=0 && objPar.limInf<=1){
	        this.limInf = objPar.limInf;
	    }
	    if (rialto.lang.isNumber(objPar.limSup) && objPar.limSup>=0 && objPar.limSup<=1){
	        this.limSup = objPar.limSup;
	    }              
    }
    else{ 
	    if (rialto.lang.isNumber(objPar.limInf)){
	        this.limInf = objPar.limInf;        
	    }
	    if (rialto.lang.isNumber(objPar.limSup)){
	        this.limSup = objPar.limSup;
	    }
    }
    if (rialto.lang.isBoolean(objPar.reverseClose)){
        this.reverseClose = objPar.reverseClose;        
    }        
    if (rialto.lang.isBoolean(objPar.withImg)){
        this.withImg = objPar.withImg;        
    }
	if (rialto.lang.isNumber(objPar.tailleCurs)){
        this.tailleCurs=objPar.tailleCurs;        
    }

	this.$$createSpliter (objPar.parent);
}



rialto.widget.Splitter.prototype = new rialto.widget.AbstractContainer;


rialto.widget.Splitter.prototype.onresizeDiv1=function(oldH,oldW,newH,newW){
//The code must be redefine after the instanciation of the component. By default this method does nothing. 
};

rialto.widget.Splitter.prototype.onresizeDiv2=function(oldH,oldW,newH,newW){
//The code must be redefine after the instanciation of the component. By default this method does nothing. 
};


// *******************************************
// GENERATION OF DOM ELEMENTS
// *******************************************

rialto.widget.Splitter.prototype.$$createSpliter=function(parent){

        var oThis=this;
        
        this.divExt.style.top= this.top;
        this.divExt.style.left= this.left;
        this.divExt.style.width= this.width;
        this.divExt.style.height= this.height;
        this.divExt.style.position=this.position;
        this.divExt.style.overflow="hidden";
        this.divExt.id="splitter";

        //Two DIV child
        this.div1=document.createElement("DIV");
        this.div1.className="Spliter-div1";
        this.div1.style.overflow = this.overflow;
        this.divExt.appendChild(this.div1);
        this.div1.oCiu = this;
        

		this.div2=document.createElement("DIV");
        this.div2.className="Spliter-div2";
        this.div2.style.overflow=this.overflow;
        this.divExt.appendChild(this.div2);
        this.div2.oCiu = this;
        	

        //Cursor
        this.Divcurs =document.createElement("DIV");
        this.Divcurs.className="Spliter-Divcurs";
        this.divExt.appendChild(this.Divcurs);
        //Button in cursor 
        this.Boutcurs =document.createElement("DIV");
        this.Boutcurs.id ="boucurseur";
        this.Boutcurs.className="Spliter-Boutcurs";

		//Image
        if (this.withImg){
            //add  image in the button
            this.img=document.createElement("IMG");
            this.img.src=rialtoConfig.pathRialtoE+ "images/tirette_"+this.orientation+".gif";
            this.Boutcurs.appendChild(this.img);
        }
        else{
            this.Boutcurs.style.background='#0099CC'
            if (this.orientation=='v'){
                this.Divcurs.style.borderTop="1px solid white";
                this.Divcurs.style.borderBottom="1px solid black";
            }
            else{
                this.Divcurs.style.borderLeft="1px solid white";
                this.Divcurs.style.borderRight="1px solid black";
            }
            this.Divcurs.style.background="#BBBBBB";
            this.img=this.Boutcurs
        }
        
        this.Divcurs.appendChild(this.Boutcurs);


        //CHANGE OF THE COLOR ON MOUSE EVENTS
        this.Boutcurs.onmouseover=function(){
            oThis.Boutcurs.style.cursor="pointer";
            if (oThis.withImg){
                oThis.img.src=rialtoConfig.pathRialtoE+ "images/tirette_"+oThis.orientation+"_on.gif";
            }
            else{
                this.style.background='#00CCFF'
            }
        }
        this.Boutcurs.onmouseout=function(){
            if (oThis.withImg){
                oThis.img.src=rialtoConfig.pathRialtoE+ "images/tirette_"+oThis.orientation+".gif";
            }
            else{
                this.style.background='#0099CC'
            }
        }

        if (parent) {this.placeIn(parent)};       

}

rialto.widget.Splitter.prototype.release=function(){        
    this.div1.oCiu = null;      
    this.div2.oCiu =null;
    this.Boutcurs.onmouseover=null;
    this.Boutcurs.onmouseout=null;
    this.img.onmousedown=null;
    this.afterDD=null;
    this.afterClic=null;
    
}


rialto.widget.Splitter.prototype.masqueContenuDiv2 =function() {
//====================================
    var nd = this.div2.firstChild;
    var ndSuiv = null;
    while (nd) {
        ndSuiv = nd.nextSibling;
        if (   nd.oCiu
            && nd.oCiu.setVisible)
            nd.oCiu.setVisible(false);
        else
            nd.parentNode.removeChild(nd);
        nd = ndSuiv;
    }
}


rialto.widget.Splitter.prototype.getHtmlDD=function(){
	return this.img;
}


rialto.widget.Splitter.prototype.adaptToContext=function() {


    var oThis=this;
	//DIFFERENT SIZE ACCORDING TO THE PROPORTION AND ORIENTATION
    if (this.orientation=='v'){
        this.Divcurs.style.width  ="100%";
        this.Divcurs.style.height =this.tailleCurs;
        ria.utils.measures.$setHeightConformW3C(this.Divcurs);       
        this.Boutcurs.style.height =this.tailleCurs;
        this.Boutcurs.style.left="40%";
        this.Boutcurs.style.width ='20%';
        this.div1.style.width="100%";
        this.div2.style.width="100%";
    }
    else{
    	this.Divcurs.style.height  ="100%";
        this.Divcurs.style.width =this.tailleCurs;        
        ria.utils.measures.$setWidthConformW3C(this.Divcurs);
        this.Boutcurs.style.top="40%";
        this.Boutcurs.style.width=this.tailleCurs;
        this.Boutcurs.style.height ='20%';
        this.div1.style.height="100%";
        this.div2.style.height="100%";
    }
    
    //INITIAL HEIGHT AND WIDTH
    if (this.autoResizableH){
	    this.divExt.style.height=this.getNewParentHeight()-this.divExt.offsetTop;
	}
    else{
        this.divExt.style.height=this.height;
    }
    if (this.autoResizableW){
        this.divExt.style.width=this.getNewParentWidth()-this.divExt.offsetLeft;
    }
    else{
        this.divExt.style.width= this.width;
    }
	 

    //Style
    
    if (this.style=="3D"){
    
        var oParent 	= this.div1;
        this.div3D1		= new objDiv3D();
        this.div3D1.createDiv3D(oParent);
        
        oParent 		= this.div2;
        this.div3D2		= new objDiv3D();
        this.div3D2.createDiv3D(oParent);

    }


    //DRAG AND DROP
    if (!this.parent.oCiu){
      this.parent.oCiu = new Array();
    }
    this.parent.oCiu['splitter'] = this;


    this.img.onmousedown  = function (e){
         stopDefault(e);
    }
    
   rialto.widgetBehavior.affect(this	
    							  ,'DragAndDrop'
    							  ,{ oHtmlToMove 			: this.Divcurs
 								    ,oHtmlEvtTarget		    : this.img
 								    ,ghost: { aspect:'rect'
 								    		 ,asChild: true }
 								    ,bSelectMark : false
    							    ,isWithLimitsDisplayed  : false
                          			,movingLimits : {orientation: oThis.orientation}
                          			}
    							  );


    // call back function in dragAndDropBehavior
    this.afterDD = function(){
        this.div1.style.overflow='hidden';
	    this.div2.style.overflow='hidden';
	    
        if (oThis.withImg){
            oThis.img.src=rialtoConfig.pathRialtoE+ "images/tirette_"+oThis.orientation+".gif";
        }
        else{
            oThis.Boutcurs.style.background='#0099CC'
        }


          if (this.orientation=='v'){
              prop=      parseInt(this.Divcurs.style.top)
                    /    (   this.divExt.offsetHeight
                          -  parseInt(this.Divcurs.style.height));
              this.fixeProp(prop,false);
          }
          else{
              prop=      parseInt(this.Divcurs.style.left)
                    /    (   this.divExt.offsetWidth
                          -  parseInt(this.Divcurs.style.width)); 
              this.fixeProp(prop,false);
          }
          this.div1.style.overflow= this.overflow;
	      this.div2.style.overflow= this.overflow;
    }

    this.afterClic = function(){
          if (this.reduce){
             //reduce mod put the old proportion
            this.reduce=false;
            this.fixeProp(this.oldProp,false);
          }
          else{
            //save the proportion
            this.oldProp=this.prop;
            this.reduce=true;
            //reduce mod
            if (this.reverseClose){this.fixeProp(1,false);}
            else{this.fixeProp(0,false);}


          }
    }    
/*    
    // modif emploi DD gen
	//rialto.widget.Splitter.prototype.initDD=function(){
    this.dragAndDropBehavior = DragAndDrop;
    this.dragAndDropBehavior(
                       {
                           objetADeplacer : this.Divcurs
                          ,objetCibleEvt  : this.img
                          ,fantome        : { aspect: 'aPlat'
                                             ,height: 'parseInt(this.Divcurs.offsetHeight)'
                                            }  //cadre
                          ,orientation    : oThis.orientation
                          ,majDyn         : true                    
                          ,modCursor      : {avantDD : false, auClic : true}
                       }
                       );


    // call back function in dragAndDropBehavior
    this.afterDD = function(){
        this.div1.style.overflow='hidden';
	    this.div2.style.overflow='hidden';
	    
        if (oThis.withImg){
            oThis.img.src=rialtoConfig.pathRialtoE+ "images/tirette_"+oThis.orientation+".gif";
        }
        else{
            oThis.Boutcurs.style.background='#0099CC'
        }


          if (this.orientation=='v'){
              prop=      parseInt(this.Divcurs.style.top)
                    /    (   this.divExt.offsetHeight
                          -  parseInt(this.Divcurs.style.height));
              this.fixeProp(prop,false);
          }
          else{
              prop=      parseInt(this.Divcurs.style.left)
                    /    (   this.divExt.offsetWidth
                          -  parseInt(this.Divcurs.style.width)); 
              this.fixeProp(prop,false);
          }
          this.div1.style.overflow= this.overflow;
	      this.div2.style.overflow= this.overflow;
    }
    
    this.afterClic = function(){
          if (this.reduce){
             //reduce mod put the old proportion
            this.reduce=false;
            this.fixeProp(this.oldProp,false);
          }
          else{
            //save the proportion
            this.oldProp=this.prop;
            this.reduce=true;
            //reduce mod
            if (this.reverseClose){this.fixeProp(1,false);}
            else{this.fixeProp(0,false);}


          }
    }    
*/
    this.fixeProp();
}

rialto.widget.Splitter.prototype.adaptAfterContainerChange = function () {
	this.fixeProp();
}

rialto.widget.Splitter.prototype.modDim = function (dHeight,impactPart) {
//=========================
//en attendant d'avoir compris l'usage possible de updateSize pour le visualisateur
    dHeight = parseInt(dHeight);
    if (!dHeight) return;
    impactPart += '';
    if ('012'.indexOf(impactPart) == -1) {//alert('modDim valeur impossible pour le 2 ieme argt');
    return;}


    var newHeight = this.divExt.offsetHeight+dHeight;
    var newProp;
    // chgt de hauteur de la div externe
    this.divExt.style.height = newHeight;
    // calcul de la nvlle proportion suivant la partie impact?e
    newProp = this.$$getNewProp ({ height : newHeight
                                 , dHeight: dHeight
                                 , part   : impactPart});
    // applique les nouvelles dimensions des 2 parties
    this.fixeProp(newProp,false);
}


rialto.widget.Splitter.prototype.$$getNewProp = function (specs) {
//===============================
    var prop = this.prop;
    var hGlob = specs.height;
    var dHeight = specs.dHeight;
    var part  = specs.part;


    if (part == 1)
        prop  = (parseInt(this.Divcurs.style.top) + dHeight)
                /    (   this.divExt.offsetHeight
                      -  parseInt(this.Divcurs.style.height));
    else
    if (part == 2)
        prop  = (parseInt(this.Divcurs.style.top) - dHeight)
                /    (   this.divExt.offsetHeight
                      -  parseInt(this.Divcurs.style.height));


    return prop;
}


rialto.widget.Splitter.prototype.updateSize= function(){       

    this.div1.style.overflow='hidden';
    this.div2.style.overflow='hidden';
    if (this.autoResizableH){            
        this.updateHeight();        
    }   
    if(this.autoResizableW){        
        this.updateWidth();
    }    
    this.div1.style.overflow= this.overflow;
    this.div2.style.overflow= this.overflow;
    
}

rialto.widget.Splitter.prototype.updateWidth=function(){  	
    //get new width of the parent
    var tailleCalc=parseInt(this.getNewParentWidth());    
    this.divExt.style.width=tailleCalc;    
    //We don't use 100% because not enought fast for chain resize
    if (this.orientation=='v'){       
        this.div1.style.width=tailleCalc;
        this.div2.style.width=tailleCalc;
    }    
    //fixe proportion according to new heigth
    this.fixeProp(this.prop,true)
    //call childs resize
    this.resizeChilds(true,false);       
}

rialto.widget.Splitter.prototype.updateHeight=function(){        
    //get new height of the parent
    var tailleCalc=parseInt(this.getNewParentHeight());
    this.divExt.style.height=tailleCalc;  

	if (this.orientation=='h'){    
        this.div1.style.height=tailleCalc;
        this.div2.style.height=tailleCalc;
    }              
    //fixe proportion according to new width
    this.fixeProp(this.prop,true)
	//call childs resize
    this.resizeChilds(false,true);       
    
}

rialto.widget.Splitter.prototype.fixeProp=function(prop,boolMajT){

    var oldProp=this.prop;
    if (prop!=null){this.prop = prop}

    //check proportion according to limit
    // if %
    if (this.modeLim == '%') {
      if (this.prop<this.limInf){this.prop=this.limInf;}
      if (this.prop>this.limSup){this.prop=this.limSup;}
    }
    //if absolute
    else {
      if ( (this.prop * parseInt(this.divExt.offsetHeight)) < this.limInf)
          this.prop = (this.limInf +1)/ parseInt(this.divExt.offsetHeight) ;
      else
      // a revoir limInf et limSup forcement li?s !!
      if ( (this.prop * parseInt(this.divExt.offsetHeight)) > (parseInt(this.divExt.offsetHeight) - this.limInf))
          this.prop = (parseInt(this.divExt.offsetHeight) - this.limInf - 1)/ parseInt(this.divExt.offsetHeight);
    }

	//DIFFERENT SIZE ACCORDING TO THE PROPORTION AND ORIENTATION

    if (this.orientation=='v'){
    	var tailleDispo=this.divExt.offsetHeight-this.Divcurs.offsetHeight;
        var tailleDiv1=Math.floor(this.prop*(tailleDispo));
        var tailleDiv2=tailleDispo-tailleDiv1;       
        //DIV1
        this.div1.style.height=tailleDiv1;
        //cursor top position
        this.Divcurs.style.top=parseInt(this.div1.style.height);
        //div2        
        this.div2.style.top=parseInt(this.Divcurs.style.top)+this.Divcurs.offsetHeight;
        this.div2.style.height=tailleDiv2;
    }
    else{
    	
	    var tailleDispo=this.divExt.offsetWidth-this.Divcurs.offsetWidth;
        var tailleDiv1=Math.floor(this.prop*(tailleDispo));
        var tailleDiv2=tailleDispo-tailleDiv1;
        //div1
        this.div1.style.width=tailleDiv1;
        //cursor left position
        this.Divcurs.style.left=parseInt(this.div1.style.width);
        //calcul de la div2
        this.div2.style.left=parseInt(this.Divcurs.style.left)+this.Divcurs.offsetWidth;
        this.div2.style.width=tailleDiv2;
    }

    this.onresizeDiv2(this.currHeightDiv2,this.currWidthDiv2,this.div2.offsetHeight,this.div2.offsetWidth);
    this.onresizeDiv1(this.currHeightDiv1,this.currWidthDiv1,this.div1.offsetHeight,this.div1.offsetWidth);
    this.currHeightDiv1=this.div1.offsetHeight;
    this.currWidthDiv1=this.div1.offsetWidth;
    this.currHeightDiv2=this.div2.offsetHeight;
    this.currWidthDiv2=this.div2.offsetWidth;

    if(!boolMajT){this.resizeChilds();}
}
